package com.ai.test;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class DownloadSharedFolderImages {

    String user = "Administrator";
    String pass ="Admin@123";

    public static void main(String[] args) {
        String folderPath = "//192.168.1.233/aa";
        File folder = new File(folderPath);
        if (!folder.isDirectory()) {
            System.out.println("指定路径不是一个文件夹！");
            return;
        }
        File[] imageFiles = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".png"));


        if (imageFiles.length == 0) {
            System.out.println("文件夹中没有jpg或png格式的图片文件！");
            return;
        }
        List<String> imageUrls = new ArrayList<>();
        for (File imageFile : imageFiles) {
            imageUrls.add("//192.168.1.233/aa/" + imageFile.getName());
        }
        String saveFolderPath = "F://";
        File saveFolder = new File(saveFolderPath);
        if (!saveFolder.exists()) {
            saveFolder.mkdirs();
        }

//        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
//        SmbFile remoteFile = new SmbFile(path,auth);
//        remoteFile.connect(); //尝试连接


        for (String imageUrl : imageUrls) {
            try {


                URL url = new URL(imageUrl);
                String fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
                File file = new File(saveFolder, fileName);
                BufferedInputStream bis = new BufferedInputStream(url.openStream());
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int count = 0;
                while ((count = bis.read(buffer, 0, 1024)) != -1) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
                bis.close();
                System.out.println(fileName + " 下载完成！");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
